package objects;

import java.awt.geom.Rectangle2D;

import Control.Zoomable;

/**
 * 
 * @author stephenbwilliamson
 */
public class ValidPosition extends Rectangle2D.Double implements
		Comparable<ValidPosition> {

	private static final long serialVersionUID = -7198510245249423649L;
	public int start;
	public int end;

	public ValidPosition(double x, double y, double width, double height) {
		super(x, y, width, height);
	}

	public ValidPosition(int start, int end) {
		this.start = start;
		this.end = end;
	}

	/**
	 * Compare to is quite skewed to match what I feel would be a simple
	 * comparison. If the start and end points of the other Valid Position lies
	 * within this Valid Position, then it will return 0.
	 * 
	 * @param obj
	 * @return
	 */
	@Override
	public int compareTo(ValidPosition vp) {
		// vp extends to the left of this (may end at the same point as this)
		if (this.start > vp.start && this.end >= vp.end) {
			return -1;
		}

		// vp extends to the right of this (may start at the same point as this)
		if (this.start <= vp.start && this.end < vp.end) {
			return 1;
		}

		return 0;
	}

	public boolean intersects(ValidPosition vp) {
		double offset = Zoomable.getInstance().getPositions();
		offset *= 3;
		if ((this.start > vp.end + offset && this.start > vp.start + offset)
				|| (this.end + offset < vp.start && this.end + offset < vp.end)) {
			return false;
		}
		return true;
	}

	@Override
	public boolean equals(Object obj) {
		ValidPosition vp = (ValidPosition) obj;
		return this.start == vp.start && this.end == vp.end;
	}

	public int getDistance() {
		return end - start;
	}

	@Override
	public String toString() {
		return "<" + start + ", " + end + ">";
	}
}
